package com.beardedcoder.utils.db.other;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DbUtils {	
	private SQLiteDatabase database;
	
	/**
	 * a context is required to perform database operations. 
	 * The context is used to open a database connection
	 * the context is not preserved, unless it's stored by the dbConnection. 
	 * @param context
	 */
	public DbUtils(Context context) {
		openDatabaseConnection(context);
	}
	
	public boolean dbConnected() {return database != null;}
	
	public void openDatabaseConnection(Context context) throws SQLiteException {
		if (!dbConnected()) {
			database = new DbHelper(context).getWritableDatabase();
		}
	}
	
	public void createTablesIfNotPresent() throws SQLiteException {
		String sql = " Create table ";
		database.execSQL(sql);
		

	    //SQL Statement to create PARKEDCARS table
	    //private static final String PARKEDCARS_CREATE = "create table " + PARKEDCARS_TABLE + " (" + KEY_CARID + " integer primary key autoincrement, " + KEY_CARMAKE + " text not null," + KEY_CARMODEL + " text not null," + KEY_CARCOLOR + " text not null," + KEY_CARLICENSENUMBER + " text not null," + KEY_CARMINUTESPARKED + "int not null);";
		
		
	}
	
	private class DbHelper extends SQLiteOpenHelper {
		private final static int DB_VERSION = 1;
		private final static String DB_NAME = "powerEnhancer";
		
		public DbHelper(Context context, CursorFactory factory) {
			super(context, DB_NAME, factory, DB_VERSION);
		}
		
		public DbHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) { }

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
	}
}
